package com.telchina.workorder.third.service.impl;


import com.telchina.common.annotation.DataSource;
import com.telchina.common.core.domain.entity.SysOrg;
import com.telchina.common.core.domain.entity.SysUser;
import com.telchina.common.enums.DataSourceType;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.framework.system.service.ISysOrgService;
import com.telchina.framework.system.service.ISysUserService;
import com.telchina.workorder.third.service.SystemThirdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 流程任务实例 Service 实现类
 *
 * @author jason
 * @author telchina
 */
@Slf4j
@Service
@DataSource(value = DataSourceType.PERMISSION)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SystemThirdServiceImpl implements SystemThirdService {


    @Autowired
    private ISysOrgService sysOrgService;

    @Autowired
    private ISysUserService userService;

    /**
     * 通过id获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser getSysUserInfo(String userId) {
        return userService.getById(userId);
    }

    /**
     * 通过id获取用户信息
     *
     * @param userIds
     * @return
     */
    @Override
    public List<SysUser> getSysUserInfoList(List<String> userIds) {
        return userService.listByIds(userIds);
    }

    /**
     * 获取登录人所在的部门id
     */
    @Override
    public String getLoginUseDeptId() {
        SysOrg sysOrg = getLoginUseDeptByUserId(SecurityUtils.getUserId());
        return sysOrg != null ? sysOrg.getId() : "";
    }

    /**
     * 通过用户id获取用户部门
     */
    @Override
    public SysOrg getLoginUseDeptByUserId(String userId) {
        // 获取登录人所在的部门 id TODO
        List<SysUser> sysUserList = userService.selectSysUserList(new SysUser() {{
            setId(userId);
        }});
        if (sysUserList.size() > 0) {
            return sysUserList.get(0).getOrg();
        } else {
            return null;
        }
    }

    /**
     * 获取指定部门下所有用户guanxi
     *
     * @param DeptId
     */
    @Override
    @DataSource(value = DataSourceType.PERMISSION)
    public List<SysUser> getSysUserByDeptId(String DeptId) {
        List<SysUser> sysUserList = userService.selectSysUserList(new SysUser() {{
            setSysOrgId(DeptId);
        }});
        return sysUserList;
    }

    @Override
    public List<SysOrg> getDeptIdInfoList(List<String> deptIds) {
        return sysOrgService.listByIds(deptIds);
    }

    /**
     * 根据角色获取用户
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysUser> getSysUserByRoleId(String roleId) {
        return userService.selectSysUserList(new SysUser() {{
            setRoleId(roleId);
        }});
    }

    /**
     * 根据传入的部门id 获取部门下所有子部门 + 传入部门的部门id
     *
     * @param deptId
     */
    static List<SysOrg> childsysOrg = new ArrayList<SysOrg>();

    @Override
    public List<String> getDeptIdByParentId(String deptId) {
        childsysOrg = new ArrayList<>();
        // 获取到全部组织机构数据
        List<SysOrg> sysOrgList = sysOrgService.selectSysOrgList(new SysOrg());
        treeSysOrgList(sysOrgList, deptId);
        List<String> result = new ArrayList<>();
        result = childsysOrg.stream().map(SysOrg::getId).collect(Collectors.toList());
        result.add(deptId);
        return result;
    }

    /**
     * 获取全部部门信息
     */
    @Override
    public List<SysOrg> getAllSysOrg() {
        return sysOrgService.selectSysOrgList(new SysOrg());
    }

    /**
     * 获取某个父节点下面的所有子节点
     *
     * @param menuList
     * @param pid
     * @return
     */
    private List<SysOrg> treeSysOrgList(List<SysOrg> sysOrgList, String pid) {
        List<SysOrg> childSysOrgs = new ArrayList<>();
        for (SysOrg sysOrg : sysOrgList) {
            //遍历出父id等于参数的id，add进子节点集合
            if (pid.equals(sysOrg.getParentId())) {
                //递归遍历下一级
                treeSysOrgList(sysOrgList, sysOrg.getId());
                childsysOrg.add(sysOrg);
            }
        }
        return childSysOrgs;
    }

}
